package online.shenjian.hadoop.flow;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @author shenjian
 * @since 2023/2/6
 */
public class FlowMapper extends Mapper<LongWritable, Text, Text, FlowBean> {

    private Text keyOut = new Text();
    private FlowBean valueOut = new FlowBean();

    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, FlowBean>.Context context) throws IOException, InterruptedException {
        String line = value.toString();
        String[] split = line.split(" ");
        String phone = split[1];
        String up = split[3];
        String down = split[4];

        keyOut.set(phone);
        valueOut.setUpFlow(Long.parseLong(up));
        valueOut.setDownFlow(Long.parseLong(down));
        valueOut.setTotalFlow();

        context.write(keyOut, valueOut);
    }
}
